package com.znsd.dining.room.utils;

import com.znsd.dining.room.properties.NodeProperties;
import com.znsd.dining.room.response.ResultData;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.NameValuePair;
import org.apache.commons.httpclient.methods.PostMethod;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;

import java.io.IOException;
import java.io.Serializable;
import java.nio.charset.StandardCharsets;
import java.util.Arrays;
import java.util.Properties;

/**
 * @ClassName SendNodeUtils
 * @Description: 短信发送
 * @Author timi
 * @Date 2020/1/2
 * @Time 17:21
 * @Version V1.0
 **/
public class SendNodeUtils implements Serializable {
    
    private static Logger logger = LoggerFactory.getLogger(SendNodeUtils.class);
    
    private static  HttpClient httpClient = new HttpClient();
    private static  Properties properties = new Properties();
    @Autowired
    private static NodeProperties nodeProperties;
    /**
     * @Author timi
     * @Description //加载配置文件
     * @Date 17:49 2020/1/2
     * @Params
     * @return
     */
    private static void loadPropertiesFile(){
        try {
            properties.load(SendNodeUtils.class.getClassLoader().getClass().getResourceAsStream(nodeProperties.getResourceFileNameUrl()));
        } catch (IOException e) {
            logger.info("加载properties文件出错了");
            e.printStackTrace();
        }
    }
    /**
     * @Author timi
     * @Description //一对一发送
     * @Date 21:00 2020/1/2
     * @Params
     * @return
     */
    public static ResultData sendNode(String phoneNumber,String context){
        PostMethod postMethod = new PostMethod(nodeProperties.getNodeUrl());
        postMethod.addRequestHeader(nodeProperties.getHeaderName(), nodeProperties.getHeadervalue());
        NameValuePair[] data = getData();
        NameValuePair[] nameValuePairs = Arrays.copyOf(data, data.length + 2);
        nameValuePairs[data.length] = new NameValuePair(nodeProperties.getSmsMob(), phoneNumber);
        nameValuePairs[data.length+1] = new NameValuePair(nodeProperties.getSmsText(), context);
        int count = sendNumber(postMethod, nameValuePairs);
        return count>0 ? ResultData.ok() : ResultData.error(getStatusMsg(count));
    }
    /**
     * @Author timi
     * @Description //批量发送（短信 轰炸）
     * @Date 21:01 2020/1/2
     * @Params
     * @return
     */
    public static ResultData bombardNode(String context, String... iphones){
        if (StringUtils.isEmpty(iphones) || iphones.length == 0){
            return ResultData.error();
        }
        PostMethod postMethod = new PostMethod(nodeProperties.getNodeUrl());
        postMethod.addRequestHeader(nodeProperties.getHeaderName(), nodeProperties.getHeadervalue());
        NameValuePair[] data = getData();
        NameValuePair[] nameValuePairs = Arrays.copyOf(data, data.length + iphones.length+1);
        nameValuePairs[data.length] = new NameValuePair(nodeProperties.getSmsText(), context);
        for (int i = 1; i <= iphones.length; i++) {
            nameValuePairs[data.length+i] = new NameValuePair(nodeProperties.getSmsMob(), iphones[i-1]);
        }
        int count = sendNumber(postMethod, nameValuePairs);
        return count>0 ? ResultData.ok() : ResultData.error(getStatusMsg(count));
    }
    
    /**
     * @Author timi
     * @Description //发送邮箱
     * @Date 21:01 2020/1/2
     * @Params
     * @return
     */
    private static int sendNumber(PostMethod postMethod, NameValuePair[] data){
        postMethod.setRequestBody(data);
        try {
            httpClient.executeMethod(postMethod);
            // 返回的状态码
            int statusCode = postMethod.getStatusCode();
            System.out.println(statusCode);
            String code = new String(postMethod.getResponseBodyAsString().getBytes(StandardCharsets.UTF_8));
//            String property = properties.getProperty(code);
//            if (StringUtils.isEmpty(property)) {
//                logger.info("成功");
//               return Integer.valueOf(code);
//            }
//            logger.info("出错了");
            return Integer.valueOf(code);
        } catch (IOException e) {
            return -66;
        }
    }
    /**
     *
     * @Author timi
     * @Description //添加基本数据
     * @Date 21:02 2020/1/2
     * @Params
     * @return
     */
    private static NameValuePair[] getData(){
        NameValuePair[]
                data = {
                new NameValuePair(nodeProperties.getUid(), properties.getProperty(nodeProperties.getUid())),
                new NameValuePair(nodeProperties.getKey(), properties.getProperty(nodeProperties.getKey())),
        };
        return  data;
    }
    
    public static String getStatusMsg(Integer code){
        String property = properties.getProperty(String.valueOf(code));
        if (StringUtils.isEmpty(property)) {
            return  null;
        }
        return property;
    }
}
